class BooksController < ApplicationController

  def new
    @book = Book.new
    @book.author_id = 1
  end

  def create
    @book = Book.new post_params
    if @book.save
      redirect_to :action => 'show', :id => @book.id
    else
      render :action => 'new'
    end
  end

  def edit
    @book = Book.find params[:id]
  end

  def update
    @book = Book.find params[:id]
    if @book.update_attributes post_params
      redirect_to :action => 'show', :id => @book.id
    else
      render :action => :edit
    end
  end

  def destroy
    @book = Book.find params[:id]
    @authorid = @book.author_id
    @book.destroy
    redirect_to author_path(:id => @authorid)
  end

  def show
    @book = Book.find params[:id]
  end

  private

  def post_params
    params.require(:book).permit(:name, :author_id, :isbn)
  end

end